package com.example.testproject.activity

import android.net.Uri
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import androidx.databinding.Observable
import com.example.testproject.R
import com.example.testproject.databinding.PreviewPictureDataBinDing
import com.example.testproject.utils.click
import com.example.testproject.utils.toast
import com.example.testproject.view_model.TestViewModel
import kotlinx.coroutines.*


/**
 *
 * @ClassName: PreviewPictureActivity
 * @Author: android 超级兵
 * @CreateDate: 12/8/21$ 1:11 PM$
 * TODO test 图片预览库
 * 官方文档: https://github.com/OneZeroYang/PreviewPictureView
 */
class PreviewPictureActivity : AppCompatActivity() {

    val vm: TestViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val dataBinDing = DataBindingUtil.setContentView<PreviewPictureDataBinDing>(
            this,
            R.layout.activity_preview_picture
        )

//        dataBinDing.vm = vm
//
//        dataBinDing.onclick = Click()


        testCoroutineScope2()

//        testCoroutineScope3()
    }

    private fun testCoroutineScope3() {
        val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
            Log.d("scope:exceptionHandler", "${coroutineContext[CoroutineName]} $throwable")
        }
        with(CoroutineScope(Dispatchers.Main + CoroutineName("scope1") + exceptionHandler + SupervisorJob())) {
            Log.d("scope", "--------- 1")
            launch(CoroutineName("scope2")) {
                Log.d("scope", "--------- 2")
                throw  NullPointerException("空指针")
                Log.d("scope", "--------- 3")
            }
            launch(CoroutineName("scope3")) {
                Log.d("scope", "--------- 4")
                delay(2000)
                launch {
                    Log.d("scope", "--------- 7")
                    TODO()
                }
                launch {
                    Log.d("scope", "--------- 8")
                }
                Log.d("scope", "--------- 5")
            }
            Log.d("scope", "--------- 6")
        }

//        CoroutineScope(Dispatchers.Main + CoroutineName("scope1") + exceptionHandler + SupervisorJob()).launch {
//            Log.d("scope", "--------- 1")
//            launch(CoroutineName("scope2")) {
//                Log.d("scope", "--------- 2")
//                throw  NullPointerException("空指针")
//                Log.d("scope", "--------- 3")
//            }
//            launch(CoroutineName("scope3")) {
//                Log.d("scope", "--------- 4")
//                delay(2000)
//
//                Log.d("scope", "--------- 5")
//            }
//            Log.d("scope", "--------- 6")
//        }
    }


    private fun testCoroutineScope2() {
        val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
            Log.d("scope:exceptionHandler", "${coroutineContext[CoroutineName]} $throwable")
        }
        GlobalScope.launch(Dispatchers.Main + CoroutineName("scope1") + exceptionHandler) {
            supervisorScope {
                Log.d("scope", "--------- 1")
                launch(CoroutineName("scope2")) {
                    Log.d("scope", "--------- 2")
                    TODO()
                    Log.d("scope", "--------- 3")
                }
                launch(CoroutineName("scope3")) {
                    Log.d("scope", "--------- 4")
                    delay(2000)
                    launch {
                        Log.d("scope", "--------- 7")
                        TODO()
                    }
                    launch {
                        Log.d("scope", "--------- 8")
                    }
                    Log.d("scope", "--------- 5")
                }
                Log.d("scope", "--------- 6")
            }
        }
    }

    inner class Click {

        fun test(view: View, str: String) {
            "打印${str}" toast view.context
        }
    }
}